﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.ProcurementsCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 开票通知单的查询处理方法
    /// </summary>
    public class InvoiceSettlementEntityQueryCommandHandler : IRequestHandler<InvoiceSettlementEntityQueryCommand, APIResult<List<InvoiceSettlementEntity>>>
    {
        /// <summary>
        /// 开票通知单仓储接口
        /// </summary>
        private readonly IBaseRepository<InvoiceSettlementEntity> iRepo;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="iRepo"></param>
        public InvoiceSettlementEntityQueryCommandHandler(IBaseRepository<InvoiceSettlementEntity>iRepo)
        {
            this.iRepo = iRepo;
        }


        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<InvoiceSettlementEntity>>> Handle(InvoiceSettlementEntityQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<InvoiceSettlementEntity>> result = new APIResult<List<InvoiceSettlementEntity>>();        
            var list = iRepo.GetAll().Result;
            if(request.DocumentCode!=null)
            {
                list = list.Where(x=>x.DocumentCode == request.DocumentCode);   
            }
            list = list.OrderByDescending(x => x.InvoiceSettlementEntityId);
            result.data = list.ToList();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.Count = list.Count();
            return await Task.FromResult(result);




        }



    }


}
